<?php

/**
 * MobileConfirmation form.
 *
 * @package    docplus.co.za
 * @subpackage form
 * @author     Fox Matuku
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class MobileConfirmationForm extends sfForm {
	private $requiredFields = array('mobile_activation_code');

	public function configure() {
		$this->widgetSchema['mobile_activation_code'] = new sfWidgetFormInputText(array(), array("autocomplete" => "off", "maxlength" => 5));
		$this->widgetSchema['id'] = new sfWidgetFormInputHidden();

		$this->validatorSchema['mobile_activation_code'] = new sfValidatorString(array("required" => true), array("required" => "Your *activation code* is required.", 'invalid' => 'Your *activation code* is invalid.'));
		$this->validatorSchema['id'] = new sfValidatorString(array("required" => true));
		$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, '_mobileActivationCodeValidator'))));

		$this->widgetSchema['mobile_activation_code']->setLabel("Activation code");

		$this->widgetSchema->setNameFormat('contact[%s]');
	}

	public function _mobileActivationCodeValidator($validator, $values, $arguments) {
		foreach ($this->requiredFields as $field) {
			try {
				$this->validatorSchema[$field]->clean($values[$field]);
			}
			catch (sfValidatorErrorSchema $e) {
				return $values;
			}
			catch (sfValidatorError $e) {
				return $values;
			}
		}

		$contact = ContactTable::getInstance()->find($values["id"]);

		if (is_object($contact) && ($contact->getMobileActivationCode() == $values["mobile_activation_code"])) {
			return $values;
		}
		else {
			// seems the supplied contact or activation code has since disappeared off our database
			$this->validatorSchema['mobile_activation_code']->setMessage('invalid', "Your *activation code*  is invalid.");
		}

		// since we've reached here seems the supplied activation code is pretty invalid
		throw new sfValidatorErrorSchema($this->validatorSchema['mobile_activation_code'], array("mobile_activation_code" => new sfValidatorError($this->validatorSchema['mobile_activation_code'], 'invalid')));
		return $values;
	}

}
